package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
@RequiredArgsConstructor
public class ShoppingCartController {

    private final ShoppingCartService shoppingCartService;

    private final DishService dishService;
    private final SetmealService setmealService;

    private final DishFlavorService dishFlavorService;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result CartAdd(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车,商品信息为：{}",shoppingCartDTO);
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        Long dishId = shoppingCartDTO.getDishId();
        Long userId = BaseContext.getCurrentId();


        //判断当前加入购物车的商品是否存在  like是模糊查询  username like #{name}
        ShoppingCart shoppingCart = shoppingCartService.lambdaQuery()
                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
                .eq(dishId != null, ShoppingCart::getDishId, dishId)
                .eq(dishFlavor != null, ShoppingCart::getDishFlavor, dishFlavor).one();
//下面应该是先查菜品，再查口味？
//        List<ShoppingCart> shoppingCarts = shoppingCartService.lambdaQuery()
////                .eq(userId != null, ShoppingCart::getUserId, userId)
//                .eq(setmealId != null, ShoppingCart::getSetmealId, setmealId)
//                .eq(dishFlavor != null, ShoppingCart::getSetmealId, dishFlavor)
//                .eq(dishId != null, ShoppingCart::getSetmealId, dishId).list();


        //如果存在，数量加一
        if (shoppingCart != null){
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartService.updateById(shoppingCart);
        }else {
            //如果不存在，插入一条购物车数据
            ShoppingCart shoppingCartAdd = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCartAdd);
            shoppingCartAdd.setUserId(userId);

            if (dishId != null){
                //插入的是菜品
                Dish dish = dishService.lambdaQuery().eq(Dish::getId, dishId).one();
                shoppingCartAdd.setName(dish.getName());
                shoppingCartAdd.setImage(dish.getImage());
                shoppingCartAdd.setAmount(dish.getPrice());

//                if (dishFlavor != null){   //口味已经在shoppingCartDTO中了
//                    shoppingCartAdd.setDishFlavor(dishFlavor);
//                }

            } else if (setmealId !=null) {
                //插入的是套餐
                Setmeal setmeal = setmealService.lambdaQuery().eq(Setmeal::getId, setmealId).one();
                shoppingCartAdd.setName(setmeal.getName());
                shoppingCartAdd.setImage(setmeal.getImage());
                shoppingCartAdd.setAmount(setmeal.getPrice());
            }

            shoppingCartAdd.setNumber(1);
            shoppingCartAdd.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCartAdd);
        }
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> listCart(){
        List<ShoppingCart> shoppingCarts = shoppingCartService.lambdaQuery().list();

        return Result.success(shoppingCarts);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //获取当前用户的购物车
        List<ShoppingCart> shoppingCarts = shoppingCartService.lambdaQuery().eq(ShoppingCart::getUserId, userId).list();
//        for (ShoppingCart shoppingCart : shoppingCarts) {
//            shoppingCartService.removeById(shoppingCart.getId());
//        }
        shoppingCartService.removeBatchByIds(shoppingCarts);//等同于上面方法？测试过，可以
        return Result.success();
    }
}
